package ui.newui;

import dbmanagement.FetchedObject;
import ui.MainView;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by IntelliJ IDEA.
 * User: [J0k3r]
 * Date: 29.11.2008
 * Time: 8:55:41
 */
public abstract class UpdatablePanel extends JPanel {
    protected ArrayList< String > updateByStrings = new ArrayList<String>();
    abstract public String[] getUpdateStrings();

    abstract public void update(UpdateTypes updateType, FetchedObject object, HashMap<String, Object> additionalParams);

    public enum UpdateTypes {
        CREATED,
        UPDATED,
        DELETED,
        ALL
    }

    protected void closeParent(Component cmp) {
        if (cmp instanceof JInternalFrame)
            ((JInternalFrame)cmp).dispose();
        else
            closeParent(cmp.getParent());
    }

    public void addUpdateByString(String updateString) {
        updateByStrings.add(updateString);
    }

    protected void doUpdate(UpdateTypes type, FetchedObject fo) {
        for (String updateByString : updateByStrings) {
            MainView.updatePanels(updateByString, type, fo, null);
        }
    }

    protected JInternalFrame getParent(Component cmp) {
        if (cmp instanceof JInternalFrame)
            return (JInternalFrame) cmp;
        else
            return getParent(cmp.getParent());
    }
}
